package com.eBook.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eBook.entity.Book;
import com.eBook.service.BookShelfService;

@Controller
@RequestMapping("/bookshelf")
public class BookShelfController {

	@Autowired
	BookShelfService bookShelfService;
	
	/**
	 * 通过uid找到书架上所有的书籍
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/{userId}", method=RequestMethod.GET)
	@ResponseBody
	public List<Book> findBookByUId(@PathVariable("userId") String userId){
		return bookShelfService.findBookByUId(userId);
	}
	
	/**
	 * 新增一本书
	 * @param userId
	 * @param bookId
	 * @return
	 */
	@RequestMapping(
			value="/{userId}/{bookId}", 
			method=RequestMethod.PUT,
			produces="text/plain;charset=utf-8")
	@ResponseBody
	public String addBookToShelf(
			@PathVariable("userId") String userId,
			@PathVariable("bookId") String bookId){
		int flag = bookShelfService.addBookToShelf(userId, bookId);
		if (flag > 0) {
			return "200";
		} else {
			return "400";
		}
	}
	
	/**
	 * 删除一本或多本
	 * @param map
	 * @return
	 */
	@RequestMapping(
			value="/",
			method=RequestMethod.DELETE)
	@ResponseBody
	public String delBookFromShelf(Map<String, Object> map){
		int flag = bookShelfService.deleteBookFromShelf(map);
		if (flag > 0) {
			return "200";
		} else {
			return "400";
		}
	}
	
	
}
